/*
 * CustomTreeCellRenderer.java
 *
 * Created on October 28, 2009, 7:13 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package agendaBD;

import java.awt.Component;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author godinho
 */
public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {
    
    private HashMap<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
    
    private ImageIcon defaultLeaf = (ImageIcon) UIManager.get("Tree.leafIcon");
    
    private ImageIcon defaultFolder = (ImageIcon) UIManager.get("Tree.openIcon");
    
    /** Creates a new instance of CustomTreeCellRenderer */
    public CustomTreeCellRenderer() {
    }
    
    public void addIcon(String title, ImageIcon ico){
        this.icons.put(title, ico);
    }   
    
    public void removeIcon(String title){
        icons.remove(title);
    }
    
    public Component getTreeCellRendererComponent
            (JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        
        Component c =
                super.getTreeCellRendererComponent(tree, value, leaf, expanded, leaf, row, hasFocus);

//        this.setFont(new Font("Arial", Font.PLAIN, 18));

        if(value instanceof Contato){
            setIcon(icons.get("contato"));
        }
        else if(icons.containsKey(value.toString())){
             setIcon(icons.get(value.toString()));
        }
        else{
             setIcon(leaf ? defaultLeaf : defaultFolder);
        }
       
        
        return c;
    }
    
}
